/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Command;
import com.bourre.events.IEvent;

import fever.commands.JSLocator;
import fever.commands.JSMethodName;
import fever.exception.IllegalArgumentException;
import fever.utils.Stringifier;

import flash.external.ExternalInterface;

/**
 * Defines and registers {@code javascript} command.
 * 
 * <p>Allow runtime js method creation and call using the ExternalInterface 
 * feature.<br />
 * If {@code ExternalInterface.available} is {@code false} commands are not 
 * executed.
 * 
 * <p>2 ways to use : 
 * <ul>
 *   <li><strong>Pixlib Command</strong> polymorphism ( {@link #execute()}.</li>
 *   <li>or call the {@link #call()} method.</li>
 * </ul>  
 * 
 * <p>All commands are registred in the {@link fever.commands.JSLocator} 
 * to easily retreives them using their {@link fever.commands.JSMethodName}
 * 
 * <p>As we want to control and keep the method name, we don't have to define the 
 * function name in javascript code.<br />
 * Example of {@link JSCommand} instanciation.
 * {@code
 *   var name : JSMethodName = new JSMethodName( "setTitle" );
 *   var command : JSCommand = new JSCommand( 
 *   	name, 
 *   	"( newtitle ){	document.title = newtitle; }" 
 *   );
 *   
 *   //result or param to pass 
 *   command.call( "newTitle" );
 * }
 * <br />The {@code function 'name_function'} is automatically added during 
 * registration process.
 * 
 * <p>Another way is to use the {@link fever.commands.JSLocator}
 * {@code
 *   var name : JSMethodName = new JSMethodName( "setTitle" );
 *   var b : Boolean = JSLocator.register( 
 *   	name, 
 *   	"( newtitle ){	document.title = newtitle; }" 
 *   );
 *   
 *   if( b ) JSLocator.getCommand( name ).call( "newtitle" );
 * }
 * 
 * @see fever.commands.JSLocator
 * @see fever.commands.JSMethodName
 *
 * @author Romain Ecarnot
 */
class fever.commands.JSCommand implements Command
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _name : String;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function JSCommand( methodName : JSMethodName, fCode : String )
	{
		if( arguments.length != 2 )
		{
			throw IllegalArgumentException( 'JS method name and code must be defined' );	
		}
		
		if( JSLocator.push( methodName, this ) )
		{
			 fCode = fCode.substring( fCode.indexOf( "(" ) );
			 var jscode : String = 'function ' + methodName + fCode;
			 var pending : Object = ExternalInterface.call( 'eval', jscode );
			 _name = methodName;
		}
	}
	
	/**
	 * Execute js command.
	 * 
	 * <p><strong>Pixlib Command</strong> polymorphism.
	 * s
	 * <p>You can override method to retreive {@code e} event parameter.
	 */
	public function execute( e : IEvent ) : Void
	{
		ExternalInterface.call( _name );
	}
	
	/**
	 * Calls js command and returns possible result.
	 * 
	 * <p>You can pass every arguments you want.
	 */
	public function call() 
	{
		return ExternalInterface.call.apply( this, [ _name ].concat( arguments ) );	
	}
	
	/**
	 * Returns the js function name used for registration.
	 */
	public function getName() : String
	{
		return _name;
	} 
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this, _name );
	}
}